package com.fr.lintcode;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

import org.junit.Test;

/**
*作者：furong
*日期：2017年2月10日
*时间：上午9:54:46
*/
public class Q131iii {
    /**
     * @param buildings: A list of lists of integers
     * @return: Find the outline of those buildings
     */
    public ArrayList<ArrayList<Integer>> buildingOutline(int[][] buildings) {
        ArrayList<ArrayList<Integer>> rs = new ArrayList<>();
        if (buildings == null || buildings.length <= 0) {
            return rs;
        }
        int minLength = Integer.MAX_VALUE;
        int maxLength = 0;
        for (int[] a : buildings) {
            if (a[0] < minLength) {
                minLength = a[0];
            }
            if (a[1] > maxLength) {
                maxLength = a[1];
            }
        }
        Tree root = new Tree();
        root.begin = minLength;
        root.end = maxLength;
        for (int[] a : buildings) {
            insert(root, a[0], a[1], a[2]);
        }
        List<Tree> child = getChild(root);
        Collections.sort(child);
        int begin = 0;
        int end = 0;
        int height = -1;
        for (Tree t : child) {
            if (t.begin == t.end) {
                continue;
            }
            if (t.height > 0) {
                if (height >= 0) {
                    if (height == t.height) {
                        end = t.end;
                    } else {
                        ArrayList<Integer> result = new ArrayList<>();
                        result.add(begin);
                        result.add(end);
                        result.add(height);
                        rs.add(result);
                        height = t.height;
                        begin = t.begin;
                        end = t.end;
                    }
                } else {
                    height = t.height;
                    begin = t.begin;
                    end = t.end;
                }
            } else {
                if (height > 0) {
                    ArrayList<Integer> result = new ArrayList<>();
                    result.add(begin);
                    result.add(end);
                    result.add(height);
                    rs.add(result);
                    height = -1;
                }
            }
        }
        if (height > 0) {
            ArrayList<Integer> result = new ArrayList<>();
            result.add(begin);
            result.add(end);
            result.add(height);
            rs.add(result);
        }
        return rs;
    }

    private List<Tree> getChild(Tree root) {
        List<Tree> rs = new ArrayList<>();
        Stack<Tree> s = new Stack<>();
        s.push(root);
        while (!s.isEmpty()) {
            Tree t = s.pop();
            if (t.hasChild) {
                if (t.left != null)
                    s.push(t.left);
                if (t.mid != null)
                    s.push(t.mid);
                if (t.right != null)
                    s.push(t.right);
            } else {
                rs.add(t);
            }
        }
        return rs;
    }

    private void insert(Tree node, int begin, int end, int height) {
        if (node.hasChild) {
            if (begin <= node.left.end) {
                if (end <= node.left.end) {
                    insert(node.left, begin, end, height);
                } else if (end <= node.mid.end) {
                    insert(node.left, begin, node.left.end, height);
                    insert(node.mid, node.left.end, end, height);
                } else {
                    insert(node.left, begin, node.left.end, height);
                    insert(node.mid, node.mid.begin, node.mid.end, height);
                    insert(node.right, node.right.begin, end, height);
                }
            } else if (begin <= node.mid.end) {
                if (end <= node.mid.end) {
                    insert(node.mid, begin, end, height);
                } else {
                    insert(node.mid, begin, node.mid.end, height);
                    insert(node.right, node.right.begin, end, height);
                }
            } else {
                insert(node.right, begin, end, height);
            }
        } else {
            if (node.height >= height) {

            } else {
                node.hasChild = true;
                Tree t1 = new Tree();
                t1.begin = node.begin;
                t1.end = begin;
                t1.height = node.height;
                node.left = t1;
                Tree t2 = new Tree();
                t2.begin = begin;
                t2.end = end;
                t2.height = height;
                node.mid = t2;
                Tree t3 = new Tree();
                t3.begin = end;
                t3.end = node.end;
                t3.height = node.height;
                node.right = t3;
            }
        }
    }

    class Tree implements Comparable<Tree> {
        int begin;
        int end;
        int height = 0;
        boolean hasChild = false;

        Tree left;
        Tree mid;
        Tree right;

        @Override
        public int compareTo(Tree o) {
            return this.begin - o.begin;
        }

    }

    @Test
    public void testA() throws IOException {
        // int[][] buildings = { { 1, 3, 3 }, { 2, 4, 4 }, { 5, 6, 1 } };
        // int[][] buildings = { { 1, 10, 3 }, { 2, 5, 8 }, { 7, 9, 8 } };
        File f = new File("C:\\Users\\Hunteron\\Downloads\\15.in");
        FileReader fr = new FileReader(f);
        @SuppressWarnings("resource")
        BufferedReader br = new BufferedReader(fr);
        String line = br.readLine();
        String[] s = line.split("},");
        int[][] buildings = new int[s.length][3];
        for (int i = 0; i < s.length; i++) {
            String n[] = s[i].split(",");
            if (n.length == 3) {
                buildings[i][0] = Integer.parseInt(n[0]);
                buildings[i][1] = Integer.parseInt(n[1]);
                buildings[i][2] = Integer.parseInt(n[2]);
            }
        }
        long begin = System.currentTimeMillis();
        ArrayList<ArrayList<Integer>> rs = buildingOutline(buildings);
        System.out.println(System.currentTimeMillis() - begin);
        System.out.println(rs);
    }
}
